import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/modify',
    component: () => import('@/views/modify/index'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'el-icon-s-home' }
    }]
  },
  {
    path: '/protocol',
    component: Layout,
    redirect: '/protocol/table',
    name: 'protocol',
    meta: { title: '网关协议', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: '协议列表',
        component: () => import('@/views/form/index'),
        meta: { title: '协议列表', icon: 'form' }
      },
      {
        path: 'video',
        name: '视频协议',
        component: () => import('@/views/form/video'),
        meta: { title: '视频协议', icon: 'el-icon-video-camera' }
      },
      {
        path: 'gpio',
        name: '添加GPIO协议',
        component: () => import('@/views/protocol/gpio'),
        meta: { title: '添加GPIO协议' },
        hidden: true
      },
      {
        path: 'uart',
        name: '添加UART协议',
        component: () => import('@/views/protocol/uart'),
        meta: { title: '添加UART协议' },
        hidden: true
      },
      {
        path: 'i2c',
        name: '添加I2C协议',
        component: () => import('@/views/protocol/i2c'),
        meta: { title: '添加I2C协议' },
        hidden: true
      },
      {
        path: 'spi',
        name: '添加SPI协议',
        component: () => import('@/views/protocol/spi'),
        meta: { title: '添加SPI协议' },
        hidden: true
      },
      {
        path: 'can',
        name: '添加CAN协议',
        component: () => import('@/views/protocol/can'),
        meta: { title: '添加CAN协议' },
        hidden: true
      },
      {
        path: 'change_gpio',
        name: '修改GPIO协议',
        component: () => import('@/views/change/gpio'),
        meta: { title: '修改GPIO协议' },
        hidden: true
      },
      {
        path: 'change_uart',
        name: '修改UART协议',
        component: () => import('@/views/change/uart'),
        meta: { title: '修改UART协议' },
        hidden: true
      },
      {
        path: 'change_i2c',
        name: '修改I2C协议',
        component: () => import('@/views/change/i2c'),
        meta: { title: '修改I2C协议' },
        hidden: true
      },
      {
        path: 'change_spi',
        name: '修改SPI协议',
        component: () => import('@/views/change/spi'),
        meta: { title: '修改SPI协议' },
        hidden: true
      },
      {
        path: 'change_can',
        name: '修改CAN协议',
        component: () => import('@/views/change/can'),
        meta: { title: '修改CAN协议' },
        hidden: true
      },
      {
        path: 'rate',
        name: '速率统计',
        component: () => import('@/views/protocol/rate'),
        meta: { title: '速率统计' },
        hidden: true
      }
    ]
  },
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'http://192.168.10.152:8888',
        meta: { title: '启动Jupyter', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
